<template>
	<div id="scoreImport">
		<div class="breadcrumb">
			<img src="../../../../static/img/outClass_icon_1.png" class="head_img">
			<span class="title">数据导出</span>
		</div>
		<div class="list">
			<div class="list_head">
				<span>请选择导出文件内容</span>
				<span class="importRecord" @click="gotoRecord()" v-if="user.type !==3">导出记录</span>
				<el-button class="import" type="primary" @click="importData">导出</el-button>
			</div>
			<div class="tips">提示：大量数据导出可能会出现延迟现象，请您耐心等待</div>
			<div class="search">
				<el-form ref="configForm" label-width="140px">
					<el-form-item label="学期:" required>
						<el-select v-model="semester" @change="changeSemester()">
							<el-option v-for='item in semesters' :label='item.semesterName' :value='item.sid' :key="item.sid">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="年级:">
						<el-select v-model="grade">
							<el-option value="0" label="全部年级"></el-option>
							<el-option value="2018" label="2018级"></el-option>
							<el-option value="2017" label="2017级"></el-option>
							<el-option value="2016" label="2016级"></el-option>
							<el-option value="2015" label="2015级"></el-option>
							<el-option value="2014" label="2014级"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="老师:" v-if="user.type !==3">
						<el-select v-model="teacher" @change="getClasses()">
							<el-option label='未选老师' value="-1"></el-option>
							<el-option v-for='item in teachers' :label='item.teacherName' :value='item.mid' :key="item.mid">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="课程:">
						<el-select v-model="course">
							<el-option label="全部课程" value='0'></el-option>
							<el-option v-for='item in courses' :label='item.name' :value='item.cid' :key="item.cid">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="开始日期:">
						<el-date-picker type='date' v-model="beginAt" placeholder="请选择开始时间" style="margin-right:10px;" class="qizhiDate"></el-date-picker>
					</el-form-item>
					<el-form-item label="结束日期:">
						<el-date-picker type='date' v-model="endAt" placeholder="请选择结束时间" class="qizhiDate"></el-date-picker>
					</el-form-item>
					<el-form-item label="导出类型:">
						<el-select v-model="type">
							<el-option value="1" label="跑步明细"></el-option>
							<el-option value="2" label="跑步统计"></el-option>
						</el-select>
					</el-form-item>
				</el-form>
			</div>
			<div class="list_head">
				<span>文件内容展示</span>
			</div>
			<div class="infoBox" v-if="type=='1'">序号 | 姓名 | 性别 | 学号 | 手机号 | 校区 | 年级 | 院系 | 注册时间 | 跑步模式 | 跑步开始时间 | 跑步结束时间 | 跑步持续时间 | 是否完成 | 本次跑步有效距离 | 奖励里程 | 步频 | 配速 | 配速是否异常
				| 课程名字 | 老师名字</div>
			<div class="infoBox" v-if="type=='2'">序号 | 姓名 | 性别 | 学号 | 手机号 | 校区 | 年级 | 院系 | 累计跑步次数 | 有效完成次数 | 累计跑步里程 | 计入成绩里程课程名字 | 老师名字</div>
		</div>
	</div>
</template>

<script>
	import { mapState, mapActions } from "vuex";

	export default {
		data() {
			return {
				list: [],
				courses: [],
				teachers: [],
				semesters: [],
				type: "2",
				grade: "0",
				course: "0",
				teacher: "",
				semester: "",
				endAt: new Date(),
				beginAt: new Date().setTime(new Date().getTime() - 30 * 3600 * 1000 * 24),
			};
		},
		computed: mapState({
			user: state => state.user
		}),
		mounted() {
			if (this.user.type == 3) {
				this.teacher = this.user.mid;
			} else {
				this.getTeachers();
			}
			//获取学期数据
			this.getSemesters();
		},
		methods: {
			//学期列表
			getSemesters() {
				let url = this.$util.domain + "/campusadminapi/v1/tools/semesters";

				this.$http({
					url,
					method: "GET"
				}).then(response => {
					if (response.data.error == 10000) {
						let semesterList = response.data.data;
						if (semesterList && semesterList.length > 0) {
							this.semester = semesterList[0].sid
							for (let i = 0; i < semesterList.length; i++) {
								this.semesters.push({
									sid: semesterList[i].sid,
									endDate: semesterList[i].endDate,
									startDate: semesterList[i].startDate,
									semesterName: semesterList[i].semesterName
								});
							}
						}
					}
				});
			},
			changeSemester() {
				let now = new Date().getTime() * 1;

				for (let i = 0; i < this.semesters.length; i++) {
					if (this.semester == this.semesters[i].sid) {
						let startDate = this.semesters[i].startDate * 1;
						let endDate = this.semesters[i].endDate * 1;
						let now = new Date().getTime();
						if (now > startDate && now < endDate) {
							this.endAt = new Date(new Date(now).format("yyyy/MM/dd 23:59:59"));
							this.beginAt = new Date(new Date(startDate).format("yyyy/MM/dd 00:00:00"));
						} else {
							this.endAt = new Date(new Date(endDate).format("yyyy/MM/dd 23:59:59"));
							this.beginAt = new Date(new Date(startDate).format("yyyy/MM/dd 00:00:00"));
						}
					}
				}
				this.getClasses();
			},
			// 教师列表
			getTeachers() {
				let url = this.$util.domain + "/campusadminapi/v1/tools/teachers";

				this.$http({
					url,
					method: "GET"
				}).then(response => {
					if (response.data.error == 10000) {
						let teacherList = response.data.data;
						if (teacherList && teacherList.length > 0) {
							this.teacher = teacherList[0].mid;
							for (let i = 0; i < teacherList.length; i++) {
								this.teachers.push({
									mid: teacherList[i].mid,
									teacherName: teacherList[i].teacherName,
								});
							}
						}
					}
				});
			},
			//课程列表
			getClasses() {
				this.courses = [];
				let url = this.$util.domain + "/campusadminapi/v1/tools/classes";
				if (!this.teacher || !this.semester) {
					this.course = "0";
					return;
				}

				this.$http({
					url,
					method: "GET",
					params: {
						mid: this.teacher,
						sid: this.semester
					}
				}).then(response => {
					if (response.data.error == 10000) {
						let coursesList = response.data.data;
						if (coursesList && coursesList.length > 0) {
							for (let i = 0; i < coursesList.length; i++) {
								this.courses.push({
									cid: coursesList[i].id,
									name: coursesList[i].name
								});
							}
						}
						this.course = "0";
					}
				});
			},
			// 导出数据
			importData() {
				let url = this.$util.domain + "/scoreexport/export";
				let startTime = new Date(this.beginAt).getTime() * 1;
				let endTime = new Date(new Date(this.endAt).format("yyyy/MM/dd 23:59:59")).getTime() * 1;
				let sname, cname, tname;
				let shijiancha = endTime - startTime;

				if (startTime > endTime) {
					this.$util.errorMsg("开始时间不能晚于结束时间");
					return;
				}
				for (let i = 0; i < this.semesters.length; i++) {
					if (this.semester == this.semesters[i].sid) {
						let semStart = this.semesters[i].startDate;
						let semEnd = this.semesters[i].endDate;
						if (startTime > semEnd || endTime < semStart) {
							this.$util.errorMsg("导出时间范围不在学期时间范围内");
							return;
						}

						if (startTime < semStart) {
							startTime = semStart
						}
						if (endTime > semEnd) {
							endTime = semEnd
						}
						sname = this.semesters[i].semesterName;
					}
				}

				if (shijiancha > 180 * 3600 * 1000 * 24) {
					this.$util.errorMsg("导出时间范围不可超过180天");
					return;
				}

				for (let i = 0; i < this.teachers.length; i++) {
					if (this.teacher == this.teachers[i].mid) {
						tname = this.teachers[i].teacherName;
					}
				}

				if (!tname) {
					tname = this.user.name;
				}

				if (this.teacher == -1) {
					tname = '';
				}

				if (this.course == "0") {
					cname = "全部课程";
				} else {
					for (let i = 0; i < this.courses.length; i++) {
						if (this.course == this.courses[i].cid) {
							cname = this.courses[i].name;
						}
					}
				}

				this.$http({
					url,
					method: "POST",
					body: JSON.stringify({
						sname: sname,
						tname: tname,
						cname: cname,
						cid: parseInt(this.course),
						endTime: parseInt(endTime),
						mid: parseInt(this.teacher),
						grade: parseInt(this.grade),
						sid: parseInt(this.semester),
						startTime: parseInt(startTime),
						exportType: parseInt(this.type),
						rootUnid: parseInt(this.user.rootUnid)
					})
				}).then(response => {
					if (response.data.type == 200) {
						this.$util.successMsg(response.data.msg);
						location.href = "/#/scoreImportRecord";
					} else {
						this.$util.errorMsg(response.data.msg);
					}
				});
			},
			gotoRecord() {
				location.href = "/#/scoreImportRecord";
			}
		},
		beforeDestroy() {
			clearInterval(this.interval);
		}
	};
</script>

<style scoped>
	#scoreImport {
		padding-top: 80px;
		height: calc(100% - 100px);
		overflow: hidden;
	}

	#scoreImport .el-form-item {
		width: 49%;
		display: inline-block;
	}

	.tips {
		color: red;
		font-size: 12px;
		margin-bottom: 15px;
	}

	.importRecord {
		position: absolute;
		right: 100px;
		color: #0398ff;
		cursor: pointer;
	}

	#scoreImport .import {
		position: absolute;
		right: 0px;
		width: 80px;
	}

	.timeSelect {
		display: inline-block;
		margin-top: 15px;
	}

	.search {
		margin: 0 0 15px 0;
	}

	.infoBox {
		color: #333333;
		font-size: 12px;
		background-color: #f2f2f2;
		min-height: 40px;
		line-height: 40px;
		padding-left: 20px;
		margin-bottom: 15px;
	}

	.qizhiDate {
		width: 217px;
	}
</style>